﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;

namespace QuanLyKhoHangVatTu
{
    public partial class HangHoaVatTu : DevExpress.XtraEditors.XtraForm
    {
        VatTu_PB vattu_pb = new VatTu_PB();
        VatTu_BUS vattu_bus = new VatTu_BUS();
        public HangHoaVatTu()
        {
            InitializeComponent();
        }
        public HangHoaVatTu(string ma, string ten, string tinhchat,  string nhom, string mota, string donvitinh, string thanbh, int soluong, string nguongoc, string kho, decimal dongiamua, decimal dongiaban)
        {
            InitializeComponent();
            txtMa.Text = ma;
            txtTen.Text = ten;
            cboTinhChat.Text = tinhchat;
            cboNhomVTHH.EditValue = nhom;
            txtMota.Text = mota;
            cboDVT.EditValue = donvitinh;
            cboTHBH.Text = thanbh;
            txtSoLuongTT.Text = soluong.ToString();
            txtNguonGoc.Text = nguongoc;
            cboKho.EditValue = kho;
            txtDonGiaMua.Text = dongiamua.ToString();
            txtDonGiaBan.Text = dongiaban.ToString();
            txtMa.Properties.ReadOnly = true;
            
        }

        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void HangHoaVatTu_Load(object sender, EventArgs e)
        {
            cboNhomVTHH.Properties.DataSource = vattu_bus.LayDSNhomVT();
            cboNhomVTHH.Properties.DisplayMember = "MANHOMVATTU";
            cboNhomVTHH.Properties.ValueMember = "MANHOMVATTU";

            cboDVT.Properties.DataSource = vattu_bus.LayDSDVT();
            cboDVT.Properties.DisplayMember = "MADONVITINH";
            cboDVT.Properties.ValueMember = "MADONVITINH";

            cboKho.Properties.DataSource = vattu_bus.LayDSKho();
            cboKho.Properties.DisplayMember = "MAKHO";
            cboKho.Properties.ValueMember = "MAKHO";

            colDonViChuyenDoi.DataSource = vattu_bus.LayDSDVT();
            colDonViChuyenDoi.DisplayMember = "MADONVITINH";
            colDonViChuyenDoi.ValueMember = "MADONVITINH";

            cboTinhChat.SelectedItem = "Vật tư hàng hóa";
            gridDonViChuyenDoi.DataSource = vattu_bus.LayDVCDdenhap();

            //if (Flag_PB.ThemSua == 1)
            //{
            //    txtMa.ResetText();
            //    txtTen.ResetText();
            //    cboTinhChat.SelectedItem = "Vật tư hàng hóa";
            //    cboNhomVTHH.ResetText();
            //    txtMota.ResetText();
            //    cboDVT.ResetText();
            //    cboTHBH.ResetText();
            //    txtSoLuongTT.Text ="0";
            //    txtNguonGoc.ResetText();
            //    cboKho.ResetText();
            //    txtDonGiaBan.Text = "0";
            //    txtDonGiaMua.Text = "0";
            //    gridDonViChuyenDoi.DataSource = vattu_bus.LayDVCDdenhap();
            //}
            //if (Flag_PB.ThemSua == 2)
            //{
            //    txtMa.Text = VatTu_PB.MaVatTu;
            //    txtTen.Text = VatTu_PB.TenVatTu;
            //    cboTinhChat.Text = VatTu_PB.TinhChat;
            //    cboNhomVTHH.Text = VatTu_PB.Nhom;
            //    txtMota.Text = VatTu_PB.MoTa;
            //    cboDVT.Text = VatTu_PB.DonViTinh;
            //    cboTHBH.Text = VatTu_PB.ThoiHanBH;
            //    txtSoLuongTT.Text = VatTu_PB.SoLuong.ToString();
            //    txtNguonGoc.Text = VatTu_PB.NguonGoc;
            //    txtMa.Properties.ReadOnly = true;
            //    cboKho.Text = VatTu_PB.MaKho;
            //    txtDonGiaMua.Text = VatTu_PB.DonGiaMua.ToString();
            //    txtDonGiaBan.Text = VatTu_PB.DonGiaBan.ToString();
            //    gridDonViChuyenDoi.DataSource = vattu_bus.TimMaVatTu(VatTu_PB.MaVatTu);
               
            //}
            if(Flag_PB.Sua ==true)
                gridDonViChuyenDoi.DataSource = vattu_bus.TimMaVatTu(VatTu_PB.MaVatTu);
           
        }
        public VatTu_PB GetValues()
        {


            VatTu_PB.MaVatTu = txtMa.Text;
            VatTu_PB.TenVatTu = txtTen.Text;
            VatTu_PB.TinhChat = cboTinhChat.Text;
            VatTu_PB.Nhom = cboNhomVTHH.Text;
            VatTu_PB.MoTa = txtMota.Text;
            VatTu_PB.DonViTinh = cboDVT.Text;
            VatTu_PB.ThoiHanBH = cboTHBH.Text;
            if (txtSoLuongTT.Text == string.Empty)
            {
                txtSoLuongTT.EditValue = "0";
            }
            VatTu_PB.SoLuong = Int32.Parse(txtSoLuongTT.Text);
            VatTu_PB.NguonGoc = txtNguonGoc.Text;
            VatTu_PB.MaKho = cboKho.Text;
            VatTu_PB.DonGiaMua = Convert.ToDecimal(txtDonGiaMua.Text);
            VatTu_PB.DonGiaBan = Convert.ToDecimal(txtDonGiaBan.Text);
            return vattu_pb;
        }

        private bool ChecksNull()
        {
            if (txtMa.Text == string.Empty)
            {
                txtMa.ErrorText = "Mã Hàng hóa vật tư không được trống";
                return false;
            }
            if (txtTen.Text == string.Empty)
            {
                txtTen.ErrorText = "Tên Hàng hóa vật tư không được trống";
                return false;
            }
            if (cboNhomVTHH.Text == string.Empty)
            {
                cboNhomVTHH.ErrorText = "Nhóm Hàng hóa vật tư không được trống";
                return false;
            }

            if (cboKho.Text == string.Empty)
            {
                cboKho.ErrorText = "Kho không được trống";
                return false;
            }
            if (cboDVT.Text == string.Empty)
            {
                cboDVT.ErrorText = "Đơn vị tính không được trống";
                return false;
            }


            if (vattu_bus.Exists(txtMa.Text))
            {
                if (Flag_PB.Sua == true)
                {
                    return true;

                }
                txtMa.ErrorText = "Mã trùng";
                return false;
            }
            return true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            GetValues();
            if (ChecksNull())
            {
                //if (Flag_PB.ThemSua == 1)
                //{
                try
                {
                    vattu_bus.ThemSuaVatTu(vattu_pb);
                    if (gridView1.DataRowCount > 0)
                    {
                        for (int i = gridView1.RowCount - 1; i >= 0; i--)
                        {
                            VatTu_PB.DonViChuyenDoi = gridView1.GetRowCellValue(i, "TENDONVICD").ToString();
                            VatTu_PB.TyLeChuyenDoi = gridView1.GetRowCellValue(i, "TYLECD").ToString();
                            VatTu_PB.MaVatTu = txtMa.Text;
                            VatTu_PB.PhepTinh = gridView1.GetRowCellValue(i, "PHEPTINH").ToString();
                            VatTu_PB.MoTa = gridView1.GetRowCellValue(i, "MOTA").ToString();
                            VatTu_PB.DonGiaBanLe = Convert.ToDecimal(gridView1.GetRowCellValue(i, "DONGIABANLE").ToString());
                            VatTu_PB.IDDVCD = gridView1.GetRowCellValue(i, "ID").ToString();
                            if (Flag_PB.Sua == true)
                            {
                                vattu_bus.SuaDVCD(vattu_pb);

                            }
                            else
                            {
                                VatTu_PB.IDDVCD = vattu_bus.NextID_TK();
                                vattu_bus.ThemDonViChuyenDoi(vattu_pb);
                            }
                        }
                        Flag_PB.Sua = false;
                    }

                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains(" data would be truncated"))
                        XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                //}
                //if (Flag_PB.ThemSua == 2)
                //{
                //    vattu_bus.SuaVatTu(vattu_pb);
                //    if (gridView1.DataRowCount > 0)
                //    {
                //        for (int i = gridView1.RowCount - 1; i >= 0; i--)
                //        {
                //            VatTu_PB.DonViChuyenDoi = gridView1.GetRowCellValue(i, "TENDONVICD").ToString();
                //            VatTu_PB.TyLeChuyenDoi = gridView1.GetRowCellValue(i, "TYLECD").ToString();
                //            VatTu_PB.MaVatTu = txtMa.Text;
                //            VatTu_PB.PhepTinh = gridView1.GetRowCellValue(i, "PHEPTINH").ToString();
                //            VatTu_PB.MoTa = gridView1.GetRowCellValue(i, "MOTA").ToString();
                //            VatTu_PB.DonGiaBanLe = Convert.ToDecimal(gridView1.GetRowCellValue(i, "DONGIABANLE").ToString());

                //            vattu_bus.SuaDVCD(vattu_pb);
                //        }
                //    }
                //}
                //DialogResult = DialogResult.OK;
                this.Close();
            }
            //else
            //{
            //    MessageBox.Show("Các trường (*) không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return;
            //}
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            txtMa.Properties.ReadOnly = false;
            GetValues();
            if (ChecksNull())
            {
                //if (Flag_PB.ThemSua == 1)
                //{
                    try
                    {
                        vattu_bus.ThemSuaVatTu(vattu_pb);
                        if (gridView1.DataRowCount > 0)
                        {
                            for (int i = gridView1.RowCount - 1; i >= 0; i--)
                            {
                                VatTu_PB.DonViChuyenDoi = gridView1.GetRowCellValue(i, "TENDONVICD").ToString();
                                VatTu_PB.TyLeChuyenDoi = gridView1.GetRowCellValue(i, "TYLECD").ToString();
                                VatTu_PB.MaVatTu = txtMa.Text;
                                VatTu_PB.PhepTinh = gridView1.GetRowCellValue(i, "PHEPTINH").ToString();
                                VatTu_PB.MoTa = gridView1.GetRowCellValue(i, "MOTA").ToString();
                                VatTu_PB.DonGiaBanLe = Convert.ToDecimal(gridView1.GetRowCellValue(i, "DONGIABANLE").ToString());
                                VatTu_PB.IDDVCD = gridView1.GetRowCellValue(i, "ID").ToString();
                                if (Flag_PB.Sua == true)
                                {
                                    vattu_bus.SuaDVCD(vattu_pb);

                                }
                                else
                                {
                                    VatTu_PB.IDDVCD = vattu_bus.NextID_TK();
                                    vattu_bus.ThemDonViChuyenDoi(vattu_pb);
                                }
                            }
                            Flag_PB.Sua = false;
                        }
                        txtMa.ResetText();
                        txtTen.ResetText();
                        cboTinhChat.ResetText();
                        cboNhomVTHH.ResetText();
                        txtMota.ResetText();
                        cboDVT.ResetText();
                        cboTHBH.ResetText();
                        txtSoLuongTT.ResetText();
                        txtNguonGoc.ResetText();
                        cboKho.ResetText();
                        txtDonGiaBan.Text = "0";
                        txtDonGiaMua.Text = "0";
                        gridDonViChuyenDoi.DataSource = vattu_bus.LayDVCDdenhap();
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains(" data would be truncated"))
                            XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                //}
                //if (Flag_PB.ThemSua == 2)
                //{
                //    txtMa.Properties.ReadOnly = false;
                //    vattu_bus.SuaVatTu(vattu_pb);
                //    if (gridView1.DataRowCount > 0)
                //    {
                //        for (int i = gridView1.RowCount - 1; i >= 0; i--)
                //        {
                //            VatTu_PB.DonViChuyenDoi = gridView1.GetRowCellValue(i, "TENDONVICD").ToString();
                //            VatTu_PB.TyLeChuyenDoi = gridView1.GetRowCellValue(i, "TYLECD").ToString();
                //            VatTu_PB.MaVatTu = txtMa.Text;
                //            VatTu_PB.PhepTinh = gridView1.GetRowCellValue(i, "PHEPTINH").ToString();
                //            VatTu_PB.MoTa = gridView1.GetRowCellValue(i, "MOTA").ToString();
                //            VatTu_PB.DonGiaBanLe = Convert.ToDecimal(gridView1.GetRowCellValue(i, "DONGIABANLE").ToString());

                //            vattu_bus.SuaDVCD(vattu_pb);
                //        }
                //    }
                //    Flag_PB.ThemSua = 1;
                //}
                
            }
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
                DataRow dvcd = gridView1.GetFocusedDataRow();
                if (dvcd == null)
                    return;

                if (dvcd["PHEPTINH"].ToString() == "Phép nhân")
                {
                    dvcd["MOTA"] = "1 " + dvcd["TENDONVICD"] + " = " + dvcd["TYLECD"] + " " + cboDVT.Text;
                    dvcd["DONGIABANLE"] = Convert.ToDecimal(txtDonGiaBan.Text) * Convert.ToDecimal(dvcd["TYLECD"].ToString());
                }
                if (dvcd["PHEPTINH"].ToString() == "Phép chia")
                {
                    dvcd["MOTA"] = "1 " + dvcd["TENDONVICD"] + " = 1/" + dvcd["TYLECD"] + " " + cboDVT.Text;
                    dvcd["DONGIABANLE"] = Convert.ToDecimal(txtDonGiaBan.Text) / Convert.ToDecimal(dvcd["TYLECD"].ToString());
                }

        }

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            DataRowView rơw = gridView1.GetFocusedRow() as DataRowView;
            if (rơw == null) return;
            rơw["TYLECD"] = "1";
            rơw["PHEPTINH"] = "Phép nhân";
            
        }

        private void gridDonViChuyenDoi_Click(object sender, EventArgs e)
        {

        }

        private void gridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void txtDonGiaMua_Leave(object sender, EventArgs e)
        {
            try
            {
                if (txtDonGiaMua.Text.Equals("0"))
                    return;
                double temp = Convert.ToDouble(txtDonGiaMua.Text);
                txtDonGiaMua.Text = temp.ToString("#,###");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex);
            }
        }

        private void txtDonGiaBan_Leave(object sender, EventArgs e)
        {
            
            try
            {
                if (txtDonGiaBan.Text.Equals("0"))
                    return;
                double temp = Convert.ToDouble(txtDonGiaBan.Text);
                txtDonGiaBan.Text = temp.ToString("#,###");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex);
            }
        }
    }
}